<template>
    <div class="studenthome">
        <div class="content">
            <header>帐号列表</header>
            <div class="fw-normal">请选择登录账号</div>
            <div class="student" v-for="(v, i) in users" @click="click(v)">
                <div class="sutdent-ceonter">
                    <div class="div" v-show="v.ID === user?.ID">
                        当前使用
                    </div>
                    <div class="text fw-bold">
                        {{ v?.name }}
                    </div>
                    <div class="text">
                        <span>账号：{{ v.ID }} / {{ v.phonenum || v.gid }}</span>
                    </div>
                </div>
            </div>
            <button class="btn d-block btn-login2" @click="$router.push({ name: '登录' })" type="button">+ 添加绑定帐号</button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter()
import { useStorage } from '@vueuse/core'
import Studycodemodel from '../../../mysql_interface/studycodemodel';
import { asyncgetzhimahuuer, asynclogin } from '../../../api';
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus';
import { Storekey } from '../../../interface';
const users = useStorage<Studycodemodel.Iuser[]>('users', [])
const user = useStorage<Studycodemodel.Iuser>('user', {})

const url = new URL(window.location.href)
const params = url.searchParams
const unionid = params.get('unionid')
const openid = params.get('openid')


async function start() {
    if (unionid) {
        const e = await asyncgetzhimahuuer({ unionid })
        const { rows } = e
        users.value = rows.map(e => {
            return {
                ID: e.userid,
                password: e.user?.password,
                phonenum: e.user?.phonenum,
                name: e.user?.name
            }
        })
    }
}


onMounted(() => {
    start()
})

const store = useStore(Storekey)

async function login(v: Studycodemodel.Iuser) {
    asynclogin({ ID: v.ID, password: v.password }).then(async (e) => {
        await store.dispatch('actiononline')
        router.push('/')
    }).catch(c => {
        ElMessage.error(c);
    })
}

function click(v) {
    login(v)

}

</script>

<style scoped>
.studenthome {
    background-size: cover;
    height: 100%;
    position: relative;
}

.content {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 50px;
}


.student {
    background-color: rgba(255, 255, 255, 0.4);
    border-radius: 5px;
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-bottom: 7px;
    width: 80%;
    height: 85px;
    padding-left: 20px;
    position: relative;
    border: 1px solid #dfdfdf;
}

.avatar {
    height: 47px;
    width: 47px;
    border-radius: 80px;
    display: inline-block;
    overflow: hidden;
    position: relative;
}

.image {
    height: 47px;
    width: 47px;
    background-position: center center;
    background-size: cover;
    background-repeat: no-repeat;
}

.sutdent-ceonter {
    border-radius: 15px;
    display: flex;
    flex-direction: column;
    padding-left: 20px;
}

.div {
    font-size: 12px;
    color: #17B44C;
    font-weight: bold;
}

.text {
    font-size: 12px;
    margin-top: 5px;
}

.addstudent {
    background-color: rgba(179, 188, 179, 0.333);
    border-radius: 15px;
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 15px;
    width: 80%;
    height: 50px;
    padding-left: 20px;
}

.addimage {
    width: 20px;
    height: 20px;
    margin-left: 17px;
    display: inline-block;
    overflow: hidden;
    position: relative;
}

.cha {
    position: absolute;
    right: 0px;
    top: 0px;
    padding: 5px 10px 10px;
    font-weight: bold;
    color: gray;
}

.logout-container {
    position: absolute;
    bottom: 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
}


.btn-login2 {
    width: 148px;
    color: #fff;
    background: #17B44C;
    border-radius: 2px;
    margin-top: 24px;
}

.content header {
    font-weight: 700;
    font-size: 36px;
    text-align: left;
    width: 80%;
    margin-bottom: 24px;

}

.fw-normal {
    width: 80%;
    font-size: 12px;
    margin-bottom: 32px;

}
</style>